from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers
from goods.search_indexes import SKUIndex
from .models import SKU


class SKUSerializer(serializers.ModelSerializer):

    class Meta:
        model = SKU
        fields = ['id','name','price','comments','default_image_url']


class SKUIndexSerializer(HaystackSerializer):
    """SKU索引结果数据序列化器"""
    object = SKUSerializer(read_only=True)

    class Meta:
        # 下面这是固定写法，需要死记，index_classes就和上面的model一样
        # SKUIndex是我们前面刚在search_indexes中创建的类
        index_classes = [SKUIndex]
        # 我们的需求值指定了这两个字段
        field = ('text','object')